package com.example.cs_jump;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;

import com.example.cs_jump.Util.HttpConnectionFilter;

import java.io.IOException;

public class StartActivity extends AppCompatActivity {
    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = View.inflate(this, R.layout.activity_start, null);

        setContentView(view);

        SharedPreferences sp = getSharedPreferences("flag", MODE_PRIVATE);
        Boolean flag = sp.getBoolean("flag", false);

        if (flag == false) {
            //启动动画
            alphaAnimation();
        } else {
            jump();
        }

    }

    private void alphaAnimation() {
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f, 1.0f);
        alphaAnimation.setDuration(5000);
        view.setAnimation(alphaAnimation);

        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

                //循环访问获取 url里 json文件 并写入
                for (int i = 0; i < HttpConnectionFilter.mapUrl.length; i++) {
                    HttpConnectionFilter.downFile(HttpConnectionFilter.mapUrl[i], StartActivity.this, i);
                }
                //设置flag 为ture
                Boolean flag = true;
                //SharedPreferences 对象 getSharedPreferences(flag,MODE_PRIVATE)
                SharedPreferences sp = getSharedPreferences("flag", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("flag", flag);
                editor.commit();
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        try {
//                           String result= HttpConnectionFilter.getMapJsonFilter(0);
//                            Log.d("tartactivity",result);
//                        } catch (IOException e) {
//                            e.printStac0Trace();
//                        }
//                    }
//                }).start();

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                jump();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }


    public void jump() {
        Intent intent = new Intent(StartActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

}